<template>
  <div class="role-list">
    <el-card
      class="box-card"
      shadow="never"
      :body-style="{ height: '0px', padding: '0px 0px' }"
      v-permission="3"
    >
      <div slot="header" class="clearfix">
        <div class="filter-container">
          <div>筛选搜索</div>
          <div>
            <el-button type="primary" @click="onSubmit" :disabled="loading"
              >查询搜索</el-button
            >
            <el-button :disabled="loading" @click="onReset">重置</el-button>
          </div>
        </div>
        <el-form
          ref="form"
          :model="form"
          :inline="true"
          label-width="140px"
          style="margin-top: 15px"
        >
          <el-form-item label="输入搜索" prop="name">
            <el-input v-model="form.name" placeholder="角色名称"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="box-card"
      shadow="never"
      :body-style="{ height: '0px', padding: '0px 0px' }"
      v-permission="5"
    >
      <div slot="header" class="clearfix">
        <el-button @click="handleAdd">添加角色</el-button>
      </div>
    </el-card>
    <el-table :data="roles" style="width: 100%" v-loading="loading" border>
      <el-table-column prop="id" label="编号" width="150" align="center" />
      <el-table-column prop="name" label="角色名称" align="center" />
      <el-table-column prop="description" label="描述" align="center" />
      <el-table-column prop="createdTime" label="添加时间" align="center" />
      <el-table-column label="操作" align="center" width="150px">
        <template slot-scope="scope">
          <div>
            <el-button
              type="text"
              @click="
                $router.push({
                  name: 'alloc-menu',
                  params: {
                    roleId: scope.row.id,
                  },
                })
              "
              v-permission="2"
              >分配菜单</el-button
            >
            <el-button
              type="text"
              @click="
                $router.push({
                  name: 'alloc-resource',
                  params: {
                    roleId: scope.row.id,
                  },
                })
              "
              v-permission="19"
              >分配资源</el-button
            >
          </div>
          <div>
            <el-button type="text" @click="handleEdit(scope.row)"
            v-permission="5"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-permission="8"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="isEdit ? '编辑角色' : '添加角色'"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <create-or-edit
        v-if="dialogVisible"
        :role-id="roleId"
        :is-edit="isEdit"
        @success="onSuccess"
        @cancel="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getRoles, deleteRole } from '@/services/role'
import { Form } from 'element-ui'
import CreateOrEdit from './form.vue'

export default Vue.extend({
  name: 'RoleList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      roles: [], // 角色列表
      form: {
        current: 1,
        size: 50,
        name: ''
      }, // 查询条件
      loading: false,
      dialogVisible: false, // 控制添加/编辑角色的对话框显示和隐藏
      roleId: -1, // 编辑角色的 ID
      isEdit: false
    }
  },

  created () {
    this.loadRoles()
  },

  methods: {
    async loadRoles () {
      this.loading = true
      const { data } = await getRoles(this.form)
      this.roles = data.data.records
      this.loading = false
    },

    onSubmit () {
      this.loadRoles()
    },

    handleEdit (role: FromServer.RoleItem) {
      this.dialogVisible = true
      this.roleId = role.id
      this.isEdit = true
    },

    handleDelete (role: FromServer.RoleItem) {
      this.$confirm('是否要删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await deleteRole(role.id)
          this.$message.success('删除成功')
          this.loadRoles()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    onReset () {
      (this.$refs.form as Form).resetFields()
      this.loadRoles()
    },

    onSuccess () {
      this.dialogVisible = false // 关闭对话框
      this.loadRoles() // 重新加载数据列表
    },

    handleAdd () {
      this.isEdit = false
      this.dialogVisible = true
    }
  }
})
</script>

<style lang="scss" scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
